package com.nefu.huiyi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nefu.huiyi.dto.Result;
import com.nefu.huiyi.entity.MedicalCompany;
import com.nefu.huiyi.service.IMedicalCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/medical-companies")
public class MedicalCompanyController {

    @Autowired
    private IMedicalCompanyService medicalCompanyService;

    // 新增
    @PostMapping
    public Result<?> addCompany(@RequestBody MedicalCompany medicalCompany) {
        boolean success = medicalCompanyService.save(medicalCompany);
        return success ? Result.success() : Result.error(500, "新增失败");
    }

    // 删除
    @DeleteMapping("/{id}")
    public Result<?> deleteCompany(@PathVariable Long id) {
        boolean success = medicalCompanyService.removeById(id);
        return success ? Result.success() : Result.error(500, "删除失败");
    }

    // 修改
    @PutMapping
    public Result<?> updateCompany(@RequestBody MedicalCompany medicalCompany) {
        boolean success = medicalCompanyService.updateById(medicalCompany);
        return success ? Result.success() : Result.error(500, "修改失败");
    }

    // 根据ID查询
    @GetMapping("/{id}")
    public Result<MedicalCompany> getCompanyById(@PathVariable Long id) {
        MedicalCompany company = medicalCompanyService.getById(id);
        return Result.success(company);
    }

    // 分页查询
    @GetMapping("/page")
    public Result<Page<MedicalCompany>> getCompanyPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String companyName) {

        // 创建分页对象
        Page<MedicalCompany> page = new Page<>(pageNum, pageSize);
        // 创建条件构造器
        QueryWrapper<MedicalCompany> queryWrapper = new QueryWrapper<>();
        // 按公司名进行模糊查询
        if (companyName != null && !companyName.isEmpty()) {
            queryWrapper.like("company_name", companyName);
        }
        // 排序条件
        queryWrapper.orderByAsc("id");

        // 将查询到的当前页数据列表和总记录数、总页数等分页信息全部封装到返回的 resultPage 对象中
        Page<MedicalCompany> resultPage = medicalCompanyService.page(page, queryWrapper);
        return Result.success(resultPage);
    }

    /**
     * 修改医药公司状态
     * @param id 公司ID
     * @param requestBody 包含新状态的请求体, e.g. { "status": 0 }
     * @return Result
     */
    @PutMapping("/{id}/status")
    public Result<?> updateCompanyStatus(@PathVariable Long id, @RequestBody Map<String, Integer> requestBody) {
        Integer status = requestBody.get("status");
        if (status == null) {
            return Result.error(400, "状态值不能为空");
        }

        MedicalCompany company = medicalCompanyService.getById(id);
        if (company == null) {
            return Result.error(404, "公司不存在");
        }

        company.setStatus(status);
        boolean success = medicalCompanyService.updateById(company);

        return success ? Result.success() : Result.error(500, "更新状态失败");
    }

    /**
     * 获取所有公司列表
     * @return 包含所有公司 {id, companyName} 的列表
     */
    @GetMapping("/list-all")
    public Result<List<MedicalCompany>> listAll() {
        List<MedicalCompany> companyList = medicalCompanyService.listAllForSelection();
        return Result.success(companyList);
    }
}
